﻿using System;
using System.Configuration;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Net;
using System.Xml;

using System.ServiceModel;
using System.ServiceModel.Description;

using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Query;
using Microsoft.Xrm.Sdk.Client;
using Microsoft.Xrm.Sdk.Discovery;
using Microsoft.Xrm.Sdk.Messages;
using Microsoft.Xrm.Sdk.Metadata;
using Microsoft.Xrm.Client;
using Microsoft.Xrm.Client.Services;
using Microsoft.Xrm.Client.Configuration;
using Microsoft.Xrm.Portal;
using Microsoft.Xrm;

using Microsoft.Crm.Sdk.Messages;
using Microsoft.Crm.Services.Utility;

namespace CRM2ORCH
{
    /// <summary>
    /// Summary description for Crm2Orch
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
    // [System.Web.Script.Services.ScriptService]
    public class Crm2Orch : System.Web.Services.WebService
    {
        public Guid contactId;
        //Organization URL
        public Uri OrganizationUri;
        public ClientCredentials creds;
        public OrganizationService service;
        public string message = string.Empty;

        //made a connection to the CRM
        //change the Parse method to catch data from web.config

        [WebMethod(Description = "This method returns all CRM Entities available")]
      
        public string getAllEntities()
        {
            try
            {
                Get get = new Get();
                message=get.getAllEntities();
            }
            catch (Exception ex)
            {
                message = ex.Message;
            }

            return message;
        }


        //apitil_incident
        //apitil_uniqueid
        //INC/1361/AP/2015

        [WebMethod(Description = "This method returns all CRM Entities matching a specific filter.")]
        public string getEntities(String entityName, String attributeName, String filter)
        {
            try
            {

                Get get = new Get();
                message = get.getEntities(entityName,attributeName,filter);

            }
            catch (Exception ex)
            {
                message = ex.Message;
            }

            return message;
        }

        [WebMethod(Description = "This method returns all Entity's Attribute.")]
 
        public string getEntityAttributes(String entityName)
        {
            message = string.Empty;
            //
            try
            {
                Get get = new Get();
                message = get.getEntityAttributes(entityName);
            }
            catch (Exception ex)
            {
                message = ex.Message;
            }
            //NEW_MOTIVOCONTATTOMACRO nome:MICROSOFT.XRM.SDK.LABEL

            //returns the message
            return message;
        }

        [WebMethod(Description = "This method returns a CRM Entity object by GUID.")]
        public string getEntityByGuid(String entityName, String objectGuid)
        {
            try
            {
                Get get = new Get();
                message = get.getEntityByGuid(entityName, objectGuid);
            }
            catch (Exception ex)
            {
                message = ex.Message;
            }

            return message;
        }

        [WebMethod(Description = "This method update a CRM Entity object's attribute, identified by GUID.")]
        public string updateEntityByGuid(String entityName, String objectGuid, String attributeName, String updateValueNameOrGuid)
        {
            try
            {
                Update update = new Update();
                message = update.updateEntityByGuid(entityName, objectGuid, attributeName, updateValueNameOrGuid);
            }
            catch (Exception ex)
            {
                message = ex.Message;
            }

            return message;
        }

        [WebMethod(Description = "This method create a new CRM Entity object")]
        public string createEntity(String entityName, String attributeName, String attributeValue)
        {
            try
            {
                Create create = new Create();
                message=create.createEntity(entityName, attributeName, attributeValue);

            }
            catch (Exception ex)
            {
                message = ex.Message;
            }

            return message;
        }

        [WebMethod(Description = "This method delete a CRM Entity object")]
        public string deleteEntityByGuid(String entityName, String entityGuid)
        {
            try
            {
                Delete delete = new Delete();
                message=delete.deleteEntityByGuid(entityName, entityGuid);

            }
            catch (Exception ex)
            {
                message = ex.Message;
            }

            return message;
        } 

        //template method
        public string Void()
        {
            try{
                Connection conn = new Connection();
                service = conn.setConnection();
            }
            catch (Exception ex)
            {
                message = ex.Message;
            }
            
            return message;
        } 
    }
}
